<?php
namespace app\api\controller;
use app\parent\api;
use app\parent\parents;
use think\Db;

class Liste extends Api{
	public function listArr(){
		$id = $this->input('id' , 0);
        $name = Db::name('house_cate')->where('id' , $id)->value('name');

        switch($name){
            case '二手房':
            $prix = input('prix');
            $aire = input('aire');
            $faire = input('faire');
            if($prix != "" || $aire != '' || $faire != ''){
                if($aire != ''){
                    $aire = explode('-',$aire);
                    $aires = (int)$aire[0];
                    $airez = (int)$aire[1];
                }else{
                    $aires = 0;
                    $airez = 1000000;
                }
                if($prix != ''){
                    $prix = explode('-',$prix);
                    $prixs = (int)$prix[0];
                    $prixz = (int)$prix[1];
                }else{
                    $prixs = 0;
                    $prixz = 1000000;
                }
                if($faire == ''){
                    $faire = '中等装修';
                }
                $data = Db::query("select * from zltc_house_info where prix >= $prixs and prix <= $prixz and aire >= $aires and aire <= $airez and faire = '$faire' order by sort desc ");
                foreach($data as $k => $v){
                    if(strstr($v['pic'] , ',')){
                        $arr = explode(',' , $v['pic']);
                        $data[$k]['pic'] = end($arr);
                    }
                    if(strstr($v['etiquette'] , ',')){
                        $arr = explode(',' , $v['etiquette']);
                        $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                    }else{
                        $data[$k]['etiquette'] = [$v['etiquette']];
                    }
                    if($v['prix'] == 0){
                        $data[$k]['position'] = mb_substr($v['position'] . $v['mezzanine'] . $v['aire'] . '㎡' . '售价面议' , 0 , 18);
                    }else{
                        $data[$k]['position'] = mb_substr($v['position'] . $v['mezzanine'] . $v['aire'] . '㎡' . '售价' . $v['prix'] . '元' , 0 , 18);
                    }
                }
                echo json_encode($data);
                exit;
                break;
            }
            $where = $this->wheresql([
                'like'=>['title','prix','aire','mezzanine','faire'],
                'time' => ['time'],
                'eq' => ['info_show'],
            ]);
            $data = Db::name('house_info')
                ->where(['cid'=>$id,'info_examiner'=>2,'info_show'=>1])
                ->where($where)
                ->field('id , title, position , etiquette , adresse , prix , pic , mezzanine , aire , faire')
                ->order('sort asc')
                ->select();
            foreach($data as $k => $v){
                if(strstr($v['pic'] , ',')){
                    $arr = explode(',' , $v['pic']);
                    $data[$k]['pic'] = end($arr);
                }
                if(strstr($v['etiquette'] , ',')){
                    $arr = explode(',' , $v['etiquette']);
                    $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                }else{
                    $data[$k]['etiquette'] = [$v['etiquette']];
                }
                if($v['prix'] == 0){
                    $data[$k]['position'] = mb_substr($v['position'] . $v['mezzanine'] . $v['aire'] . '㎡' . '售价面议' , 0 , 18);
                }else{
                    $data[$k]['position'] = mb_substr($v['position'] . $v['mezzanine'] . $v['aire'] . '㎡' . '售价' . $v['prix'] . '元' , 0 , 18);
                }
            }

            echo json_encode($data);
            exit;
            break;
        case '新盘':
            $where = $this->whereSql([
                'like'=>['title'],
                'time' => ['time'],
                'eq' => ['plat_show'],
            ]);
            $data = Db::name('plat_info')
                ->where($where)
                ->where('plat_show',1)
                ->order('sort asc')
                ->select();

            foreach($data as $k => $v){
                if(strstr($v['pic'] , ',')){
                    $arr = explode(',' , $v['pic']);
                    $data[$k]['pic'] = end($arr);
                }
                if(strstr($v['etiquette'] , ',')){
                    $arr = explode(',' , $v['etiquette']);
                    $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                }
            }
            echo json_encode($data);
            exit;
            break;
         
        case '出租房':

            $prix = input('prix');
            $aire = input('aire');
            $mode = input('mode');
            if($prix != "" || $aire != '' || $mode != ''){
                if($aire != ''){
                    $aire = explode('-',$aire);
                    $aires = (int)$aire[0];
                    $airez = (int)$aire[1];
                }else{
                    $aires = 0;
                    $airez = 1000000;
                }
                if($prix != ''){
                    $prix = explode('-',$prix);
                    $prixs = (int)$prix[0];
                    $prixz = (int)$prix[1];
                }else{
                    $prixs = 0;
                    $prixz = 1000000;
                }
                if($mode == ''){
                    $mode = '付三押一';
                }
                // dump($prixz .'+'. $prixs .'+'. $aires .'+'. $airez .'+'. $mode);die;
                $data = Db::query("select * from zltc_louer_info where prix >= $prixs and prix <= $prixz and aire >= $aires and aire <= $airez and mode = '$mode' order by sort desc ");
                // return Db::getLastSql($data);
                foreach($data as $k => $v){
                    if(strstr($v['pic'] , ',')){
                        $arr = explode(',' , $v['pic']);
                        $data[$k]['pic'] = end($arr);
                    }
                    if(strstr($v['etiquette'] , ',')){
                        $arr = explode(',' , $v['etiquette']);
                        $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                    }
                }
                echo json_encode($data);
                exit;
                break;
            }
            $where = $this->whereSql([
                'like'=>['title','prix','aire','mode','faire'],
                'time' => ['time'],
                'eq' => ['info_show'],
            ]);

            $data = Db::name('louer_info')
                ->where(['cid'=>$id,'info_examiner'=>2,'info_show'=>1])
                ->where($where)
                ->order('sort asc')
                ->select();
            
            foreach($data as $k => $v){
                if(strstr($v['pic'] , ',')){
                    $arr = explode(',' , $v['pic']);
                    $data[$k]['pic'] = end($arr);
                }
                if(strstr($v['etiquette'] , ',')){
                    $arr = explode(',' , $v['etiquette']);
                    $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                }
            }
            echo json_encode($data);
            exit;
            break;
     
        
        case '商铺':
            $prix = input('prix');
            $aire = input('aire');
            $types = input('types');
            if($prix != "" || $aire != '' || $types != ''){
                if($aire != ''){
                    $aire = explode('-',$aire);
                    $aires = (int)$aire[0];
                    $airez = (int)$aire[1];
                }else{
                    $aires = 0;
                    $airez = 1000000;
                }
                if($prix != ''){
                    $prix = explode('-',$prix);
                    $prixs = (int)$prix[0];
                    $prixz = (int)$prix[1];
                }else{
                    $prixs = 0;
                    $prixz = 1000000;
                }
                if($types == ''){
                    $types = 1;
                }
                // dump($prixz .'+'. $prixs .'+'. $aires .'+'. $airez .'+'. $mode);die;
                $data = Db::query("select * from zltc_house_store where prix >= $prixs and prix <= $prixz and aire >= $aires and aire <= $airez and types = '$types' order by sort desc ");
                // return Db::getLastSql($data);
                foreach($data as $k => $v){
                    if(strstr($v['pic'] , ',')){
                        $arr = explode(',' , $v['pic']);
                        $data[$k]['pic'] = end($arr);
                    }
                    if(strstr($v['etiquette'] , ',')){
                        $arr = explode(',' , $v['etiquette']);
                        $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                    }
                }
                echo json_encode($data);
                exit;
                break;
            }

        
            $where = $this->whereSql([
                'like'=>['title','prix','aire'],
                'time'=>['time'],
                'eq'=>['plat_show','types'],
            ]);
            $data = Db::name('house_store')
                ->where(['plat_show'=>1,'plat_examiner'=>2,'cid'=>$id])
                ->where($where)
                ->order('sort asc')
                ->select();
            foreach($data as $k => $v){
                if(strstr($v['pic'] , ',')){
                    $arr = explode(',' , $v['pic']);
                    $data[$k]['pic'] = end($arr);
                }
                if(strstr($v['etiquette'] , ',')){
                    $arr = explode(',' , $v['etiquette']);
                    $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
                }
            }
            echo json_encode($data);
            exit;
            break;
        case '楼市资讯':
            $data = Db::name('ressources_info')->where('is_show',1)->select();

            foreach($data as $k => $v){
                $data[$k]['time'] = date('m-d' , $v['time']);
            }
            echo json_encode($data);
            exit;
            break;
            default :
            die;
        }
    }
    
    public function guanzhu(){
        $openid = $this->input('openid',0);
        $data = Db::name('house_guanzhu')->where('openid',$openid)->select();
        foreach($data as $k => $v){
            if(strstr($v['pic'] , ',')){
                $arr = explode(',' , $v['pic']);
                $data[$k]['pic'] = end($arr);
            }
            if(strstr($v['etiquette'] , ',')){
                $arr = explode(',' , $v['etiquette']);
                $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
            }
        }
        return $this->ajaxReturn($data);
    }

    public function xinpan(){
        $openid = $this->input('openid',0);
        $data = Db::name('house_xinpan')
        ->where('openid',$openid)
        ->field('*,FROM_UNIXTIME(time,"%Y-%m-%d %h:%i:%s") as time')
        ->select();
        foreach($data as $k => $v){
            if(strstr($v['pic'] , ',')){
                $arr = explode(',' , $v['pic']);
                $data[$k]['pic'] = end($arr);
            }
            if(strstr($v['etiquette'] , ',')){
                $arr = explode(',' , $v['etiquette']);
                $data[$k]['etiquette'] = array_slice($arr , 0 , 3);
            }
        }
        return $this->ajaxReturn($data);
    }

    public function person(){
        $openid = $this->input('openid',0);
        $data = Db::name('house_user')
            ->where('x_openid',$openid)
            ->where('is_personnel',1)
            ->find();
        if($data){
            return $this->ajaxReturn(['msg'=>0],'操作成功');
        }else{
            return $this->ajaxReturn(['msg'=>1],'操作失败');
        }
    }

    public function client(){
        $openid = $this->input('openid',0);
        $data = Db::name('house_user')
            ->where(['x_openid'=>$openid,'is_personnel'=>1])
            ->value('id');
        $is = Db::name('house_jilu')
            ->where(['id_user'=>$data,'guan_id'=>$data])
            ->field('*,FROM_UNIXTIME(time,"%m-%d %h:%i:%s") as time')
            ->limit(100)
            ->order('time desc')
            ->select();
        if($is){
            return $this->ajaxReturn($is,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
    }

    public function apply(){
        $openid = input('openid');
        $data['nickname'] = input('nickname');
        $data['phone_user'] = input('phone_user');
        $data['realty'] = input('realty');
        $is = Db::name('house_user')->where('x_openid',$openid)->find();
        if($is['apply'] == 1){
            return $this->ajaxReturn(1,'已申请员工');
        }else if($is['is_personnel'] == 0){
            $apply = Db::name('house_user')
                ->where('x_openid',$openid)
                ->setField(['apply'=>1,'nickname'=>$data['nickname'],'phone_user'=>$data['phone_user'],'realty'=>$data['realty']]);
            return $this->ajaxReturn(0,'申请成功');
        }else if($is['is_personnel'] == 1){
            return $this->ajaxReturn(2,'已是员工');
        }
    }

    /**
    * showdoc
    * 组成列表页查询条件
    * like , time , eq = 三个条件类型
    * ['like' => ['key1','key2']]
    */
    public function whereSql($arr)
    {
        $where = [];
        $info = $this->input();

        if(isset($arr['eq']) && is_array($arr['eq'])){

            foreach($arr['eq'] as $v){
                if(isset($info[$v]) && $info[$v] != ''){
                    $where[] = $v . '=' . $info[$v];
                }
            }

        }
        
        if(isset($arr['like']) && is_array($arr['like'])){
            
            foreach($arr['like'] as $v){
                if(isset($info[$v]) && $info[$v] != ''){
                    $where[] = $v . " like '%{$info[$v]}%'";
                }
            }

        }
        
        if(isset($arr['time']) && is_array($arr['time'])){
            
            foreach($arr['time'] as $v){
                if(isset($info[$v]) && $info[$v] != ''){
                    $d = $this->whereSqlDate($info[$v]);
                    $where[] = "{$v} >= {$d[0]} and {$v} <= {$d[1]}";
                }
            }
        
        }

        if($where){
            $where = implode(' and ',$where);
        }else{
            $where = '';
        }

        return $where;
    }
}
